package ClientController;

import java.io.IOException;
import java.io.ObjectOutputStream;

import javax.swing.SwingUtilities;

import Client.Client;
import ClientUI.ClientGUI;
import ClientUI.LoginFrame;
import Protocol.admin.AdminBehave_Q;
import Protocol.app.FileDelete_Q;

import Protocol.noticeMsgWord.NoticeMsgWord_Q;
import Protocol.noticeMsgWord.NoticeMsgWord_R;
import Protocol.threadStatus.ThreadStatus_R;
import Protocol.user.BasePersonInfo_Q;
import Protocol.user.Login_Q;
import Protocol.utility.InitContent;

public class HandlerForServer {
	Client      client;
	ClientGUI   clientGui;
	
	HandlerForServer(Client  c) {
		client = c;
		clientGui = c.getClientGui();
	}
	public void handle(Object  msg) {
		
		if(msg instanceof Login_Q) {
			handleLoginReply((Login_Q) msg);
		}
		if(msg instanceof InitContent) {
			handleInitContent((InitContent)msg);
		}
		else if(msg instanceof BasePersonInfo_Q) {
			handleBaseInfoReply((BasePersonInfo_Q)msg);
		}
		else if(msg  instanceof NoticeMsgWord_Q) {
			handleNoticeMsgWord_Q((NoticeMsgWord_Q)msg);
		}
		else if(msg instanceof NoticeMsgWord_R) {
			handleNoticeMsgWord_R((NoticeMsgWord_R)msg);
		}
		else if(msg instanceof ThreadStatus_R) {
			handleTOLR((ThreadStatus_R)msg);
		}
		else if(msg instanceof FileDelete_Q) {
			handleFileDelete_Q((FileDelete_Q)msg);
		}
		else if(msg instanceof AdminBehave_Q) {
			handleAdminBehaveQ((AdminBehave_Q)msg);
		}
		
	}
	private void handleAdminBehaveQ(AdminBehave_Q abq) {
		/*System.out.println(abq.getContact());
		if(abq != null) {
			this.clientGui.mainFrame.realContactPanel.updateContact(abq.getContact());
			this.client.getClientData().setSelf_contact(abq.getContact());
			this.clientGui.mainFrame.realMySpacePanel.contactPanel.updateContact(abq.getContact());
		}*/
		
	}
	private void handleFileDelete_Q(FileDelete_Q  fdq) {
		System.out.println(fdq.getStatus());
		//clientGui.mainFrame.realMySpacePanel.doDeleteFileResponse(fdq);
	}
	private void handleTOLR(ThreadStatus_R nr) {
		/*clientGui.mainFrame.realBbsPanel.addThread(nr.getThreadLogStatus());*/
		
	}
	private void handleNoticeMsgWord_Q(NoticeMsgWord_Q    nmwq){
		
	}
	private void handleNoticeMsgWord_R(NoticeMsgWord_R  nr) {
		/*clientGui.mainFrame.realMsgPanel.addNotice(nr.getNotice());*/
		
	}
	/*private void handleChatMsg(ChatMsg_R  cm) {
		boolean	   isDlgExisting = false;
		//System.out.println("received ChatMsg_R : "+  cm.getChatMsg().getContent()+ " from "+ cm.getChatMsg().getFrom_id());
		
		for(int i=0; i<clientGui.mainFrame.realContactPanel.getChatDialogList().size(); i++) {
			if(clientGui.mainFrame.realContactPanel.getChatDialogList().get(i).getTargetBpi().getUser_id() == cm.getChatMsg().getFrom_id()) {
				clientGui.mainFrame.realContactPanel.getChatDialogList().get(i).setCurrentMsg(cm);
				isDlgExisting = true;
				break;
			}
		}
		if(!isDlgExisting) {
			clientGui.mainFrame.tipPanel.addNewMsg(cm);
		}
		
		
	}*/
	private void handleBaseInfoReply(BasePersonInfo_Q bpi_q) {
		/*if(bpi_q.getBpi() == null) {
			clientGui.displayBaseInfoSaveStatus();      // display the status of client's baseinfo save operation
		} else {
			clientGui.displayBaseInfo();               // show personal base info
		}*/
	}
	private void handleInitContent(InitContent init) {
		this.client.getClientData().setSelf_contact(init.getContact());
		this.client.getClientData().setThreadStatusList(init.getInitialThreadStatus());
		this.client.getClientData().setFileNameLoadedList(init.getServerFileList());
		this.client.getClientData().setMsgList(init.getUnreadMsgList());
		
		//enter  main frame
		this.clientGui.enterMainFrame();	
	}
	private void handleLoginReply(Login_Q   lq) {
		if(lq.getLogin_result().contains("success")) {
			client.getClientData().setSelf_basic(lq.getClientBaseInfo());
			client.getClientData().setBpiList(lq.getDefaultUserList());				
		} else {
			clientGui.displayLoginError();
		}
	}

}
